package main.MysqlToOracle;

import java.sql.*;
import java.util.Scanner;

public class MysqlMetaData {

    // 数据库连接信息
    private static final String URL = "jdbc:mysql://11111.4:3306/11";
    private static final String USER = "111";
    private static final String PASSWORD = "1111";


    public MysqlMetaData() throws SQLException {
    }


    public void printMetaData() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 建立数据库连接
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
            statement  = connection.createStatement();


            // 执行查询
            String sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'ads' ";
            resultSet = statement.executeQuery(sql);

            // 获取元数据
            // 获取结果集的元数据，用于了解结果集的结构信息
            ResultSetMetaData metaData = resultSet.getMetaData();
            // 获取结果集中的列数，以便后续迭代处理每一列
            int columnCount = metaData.getColumnCount();


            // 获取元数据的值
            while (resultSet.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnName(i);
                    // 获取当前列名为TABLE_NAME的列值，拿到表名及表注释
                    if ("TABLE_NAME".equalsIgnoreCase(columnName)){
                        String columnValue_tableName = resultSet.getString(columnName);
                        System.out.println(columnName + ": " + columnValue_tableName);
                    } else if ("TABLE_COMMENT".equalsIgnoreCase(columnName)) {
                        String columnValue_tableComment = resultSet.getString(columnName);
                        System.out.println(columnName + ": " + columnValue_tableComment);
                    }
                }
                System.out.println("------------------------------------------");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (resultSet != null) resultSet.close();
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        MysqlMetaData metaData = new MysqlMetaData();
//        metaData.printMetaData();
    }
}
